package com.example.baguwen.service.impl;

import com.example.baguwen.common.PageResult;
import com.example.baguwen.dto.BaguwenDTO;
import com.example.baguwen.entity.Baguwen;
import com.example.baguwen.repository.BaguwenRepository;
import com.example.baguwen.service.BaguwenService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityNotFoundException;

@Service
public class BaguwenServiceImpl implements BaguwenService {

    @Autowired
    private BaguwenRepository baguwenRepository;

    @Override
    @Transactional
    public Baguwen add(BaguwenDTO baguwenDTO) {
        Baguwen baguwen = new Baguwen();
        BeanUtils.copyProperties(baguwenDTO, baguwen);
        return baguwenRepository.save(baguwen);
    }

    @Override
    @Transactional
    public Baguwen update(Long id, BaguwenDTO baguwenDTO) {
        Baguwen baguwen = baguwenRepository.findById(id)
                .orElseThrow(() -> new EntityNotFoundException("八股文不存在，ID: " + id));
        
        BeanUtils.copyProperties(baguwenDTO, baguwen);
        baguwen.setId(id); // 确保ID不被覆盖
        
        return baguwenRepository.save(baguwen);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        if (!baguwenRepository.existsById(id)) {
            throw new EntityNotFoundException("八股文不存在，ID: " + id);
        }
        baguwenRepository.deleteById(id);
    }

    @Override
    public Baguwen getById(Long id) {
        return baguwenRepository.findById(id)
                .orElseThrow(() -> new EntityNotFoundException("八股文不存在，ID: " + id));
    }

    @Override
    public PageResult<Baguwen> list(int pageNum, int pageSize) {
        // 创建分页请求
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, Sort.by(Sort.Direction.DESC, "createTime"));
        
        // 查询分页数据
        Page<Baguwen> page = baguwenRepository.findAll(pageable);
        
        // 返回分页结果
        return new PageResult<>(
                page.getContent(),
                page.getTotalElements(),
                pageNum,
                pageSize
        );
    }

    @Override
    public PageResult<Baguwen> listByType(String type, int pageNum, int pageSize) {
        // 创建分页请求
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, Sort.by(Sort.Direction.DESC, "createTime"));
        
        // 按类型查询分页数据
        Page<Baguwen> page = baguwenRepository.findByType(type, pageable);
        
        // 返回分页结果
        return new PageResult<>(
                page.getContent(),
                page.getTotalElements(),
                pageNum,
                pageSize
        );
    }

    @Override
    public PageResult<Baguwen> search(String keyword, int pageNum, int pageSize) {
        // 创建分页请求
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, Sort.by(Sort.Direction.DESC, "createTime"));
        
        // 按关键字搜索分页数据
        Page<Baguwen> page = baguwenRepository.searchByKeyword(keyword, pageable);
        
        // 返回分页结果
        return new PageResult<>(
                page.getContent(),
                page.getTotalElements(),
                pageNum,
                pageSize
        );
    }
} 